package com.zzyl.nursing.service.impl;

import java.time.*;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.vo.TimeAndCount;
import com.zzyl.serve.domain.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        reservation.setStatus(0);
        reservation.setUpdateBy(UserThreadLocal.getUserId().toString());
        reservation.setCreateBy(UserThreadLocal.getUserId().toString());
        reservation.setCreateTime(new Date());
        //reservation.setUpdateTime(new Date());
        int i = reservationMapper.insertReservation(reservation);
        return i;
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询预约信息列表
     *
     * @param dto
     * @return
     */
    @Override
    public R<PageResult> getReservationList(ReservationDto dto) {
        IPage<Reservation> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<Reservation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(dto.getStatus() != null, Reservation::getStatus, dto.getStatus());
        IPage<Reservation> reservations = page(page, queryWrapper);

        return R.ok(new PageResult(reservations.getTotal(), reservations.getRecords()));
    }

    /**
     * 获取已取消预约数量
     *
     * @return
     */
    @Override
    public R<Integer> getCancelledReservationCount() {
        //获取当天的开始时间和结束时间
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime endTime = startTime.plusDays(1);
        //获取用户id
        Long userId = UserThreadLocal.getUserId();
        //查询符合条件的记录数
        Integer count = reservationMapper.getCount(startTime, endTime, userId);
        return R.ok(count);
    }

    /**
     * 更新预约状态(取消预约)
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult updateStatus(Long id) {
        //判断当天的取消预约数量是否大于等于3
        R<Integer> r = getCancelledReservationCount();
        Integer count = r.getData();
        if (count >= 3) {
            throw new BaseException("已取消预约次数达到上限");
        }
        //更改预约状态和时间
        LocalDateTime updateTime = LocalDateTime.now();
        reservationMapper.updateStatus(id, updateTime);
        return AjaxResult.success();
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public R<List<TimeAndCount>> countByTime(long time) {
        //将time转为LocalDateTime类型
        //LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        //获取当天的开始时间和结束时间
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0, 0));
        LocalDateTime endTime = startTime.plusDays(1);
        //预约时间在当天
        List<TimeAndCount> timeAndCounts = reservationMapper.countByTime(startTime, endTime);
        return R.ok(timeAndCounts);
    }

    @Override
    public void updateReservationStatus() {
        reservationMapper.updateReservationStatus();
    }
}
